#include <vector>
#include <iostream>
#include <chrono>
#include <cstdlib> // int abs(int)
#include "player.h"
using namespace std;

//ASCII numbers
#define KEY_0 48
#define KEY_1 49
#define KEY_2 50
#define KEY_3 51
#define KEY_4 52
#define KEY_5 53
#define KEY_6 54
#define KEY_7 55

#define KEY_A 97
#define KEY_D 100
#define KEY_S 115
#define KEY_W 119

player::player(const string& player_name, const string& city_name):
	_name(player_name), _city(city(city_name, 1000)){}

void player::menu(){
	char option = 0;
	int option_i = 0;
	do{

		cout << "cursor position x: " << _city.get_cursor().posx() << endl;
		cout << "cursor position y: " << _city.get_cursor().posy() << endl;

		cout << "menu" << endl;
		cout << "0.- move cursor\n1.- build\n2.- time\n3.- show map\n4.- save\n5.- get loan\n6.- Set pipe\n7.- city expansion\nWASD.- move cursor" << endl;
		cin >> option;
		option_i = int(option);
		switch(option_i){
			case KEY_0:{
			  
				cout << "0.- up\n1.- left\n2.- down\n3.-right\n4.- no" << endl;
				int option;
				cin >> option;
				switch(option){
					case 0:this->_city.cursor_up();break;
					case 1:this->_city.cursor_left();break;
					case 2:this->_city.cursor_down();break;
					case 3:this->_city.cursor_right();break;
				}

			};break;
			case KEY_1:{
				
				for(unsigned i = 0; i < _city.get_buildings().size(); ++i){
					cout << i << ".-" << _city.get_buildings()[i].get_name() << endl;
				}
				
				int option;
				cin >> option;
				if(option >= 0 and option < BUILDING_TYPES_SIZE){
					cout << "0.- one \n1.- region\n2.- exit"
						<< endl;
					int second_option;
					cin >> second_option;
					cursor initial_cursor = _city.get_cursor();
					if(second_option == 0){
						_city.set_cell(_city.get_buildings()[option]);
					}else if(second_option == 1){

						int x_i = _city.get_cursor().posx();
						int y_i = _city.get_cursor().posy();
						int x;
						cout << "Write x position for bottom right cell" <<endl;
						cin >> x;
						cout << "Write y position for bottom right cell" <<endl;
						int y;
						cin >> y;
						while(x_i <= x){
							int temp_y = y_i;
							while(temp_y <= y){
								int size = _city.get_size();
								cursor c(x_i, temp_y, size, size);
								_city.set_cursor(c);
								_city.set_cell(_city.get_buildings()[option]);
								++temp_y;
							}
							++x_i;
						}
						_city.set_cursor(initial_cursor);
						
					}
				
				}
			};break;
			case KEY_2:{

				cout << "0.- stop\n1.- slow\n2.- normal\n3.- fast\n4.- faster\n5.-exit"
					<< endl;
				cin >> option;
				switch(option){
					case 0:_city.set_time(0);break;
					case 1:_city.set_time(16);break;
					case 2:_city.set_time(8);break;
					case 3:_city.set_time(4);break;
					case 4:_city.set_time(2);break;
				}

			};break;
			case KEY_4:{

				_city.save();

			};break;
			case KEY_5:{

				_city.get_loan();

			};break;
			case KEY_6:{

				_city.set_pipe();

			};break;
			case KEY_7:{

				_city.pay_bigger_city();

			};break;

			case KEY_A: _city.get_cursor().left();break;
			case KEY_D: _city.get_cursor().right();break;
			case KEY_S: _city.get_cursor().down();break;
			case KEY_W: _city.get_cursor().up(); break;
		
		}
	}while(option != KEY_3);
}
void player::start_city(){
	int bankruptcy_months = 0;
	int days = 0;
	do{

		print_city();

		while(days >= 30){
			_city.set_money(_city.get_money() + _city.income());
			days -= 30;
			if(_city.get_money() < -1000){
				++bankruptcy_months;
				cout << "You have been " << bankruptcy_months << 
				  " months in bankruptcy" << endl;
			}else
				bankruptcy_months = 0;
		}

		cout << "Needed buildings" << endl;
		cout << "R: " << _city.get_needed_residentials() << endl;
		cout << "C: " << _city.get_needed_shops() << endl;
		cout << "I: " << _city.get_needed_industry() << endl;

		cout << "Money: " << _city.get_money() << endl;
		cout << "Population : " << _city.get_population() << endl;
		
		chrono::high_resolution_clock::time_point first_time =
			chrono::high_resolution_clock::now();

		menu();

		chrono::high_resolution_clock::time_point last_time =
			chrono::high_resolution_clock::now();
		
		chrono::seconds int_seconds =
			chrono::duration_cast<chrono::seconds>(last_time - first_time);
		long seconds_passed = int_seconds.count() * 20;
		seconds_passed += _city.get_time();  			//Force next day
		cout << "time: " << seconds_passed << " days: " << days << endl;

		while(_city.get_time() > 0 && seconds_passed > _city.get_time()){
			_city.set_population(0);
			_city.nextGeneration();
			seconds_passed -= _city.get_time();
			++days;
		}
	}while(bankruptcy_months < 10);
	if(bankruptcy_months >= 10)
		cout << "Game over" << endl;
	else
		cout << "Couldn't load database for CrossCity" << endl;
}
